package com.jike.jkgame.data;

import java.util.Timer;
import java.util.TimerTask;

public class UserStatus {
	
	private static final long TICK_INTERVEL = 500;
	private static Timer mTimer = new Timer();
	
	private static UserStatus mInstance;
	
	private long mUid = 262145;
	private String mName;
	private int mLevel = 1;
	
	private int mFullExperience = 1000;
//	private int mMaxVigour = 100;
//	private int mMaxEnergy = 100;
	
	private Status mVigour  = new Status("Vigour");
	private Status mEnergy = new Status("Energy");
	private int mExperience = 100;
	private int mMoney = 10;
	private int mCash = 10;
	
	private long energy_ts; // 上次精力更新时间
	private long vigour_ts; //  上次体力更新时间
	private long cur_time;  // 服务器当前时间
	
	private UserStatus(){
	}
	
	public static UserStatus get() {
		if (mInstance == null){
			mInstance = new UserStatus();
//			UserStatusDAO dao = new UserStatusDAO(JKApp.get());
//			dao.open();
//			dao.restoreUserStatus();
//			dao.close();
			mInstance.startTimer();
		} 
		return mInstance;
	}
	
	public boolean isLogedin() {
		return mUid > 0;
	}
	
	public long getUid() {
		return mUid;
	}
	
	public void setUid(long id) {
		this.mUid = id;
	}
	
	public int getLevel() {
		return mLevel;
	}
	
	public void startTimer(){
		mTimer.schedule(new TimerTickService(), 0, TICK_INTERVEL);
	}
	
	public void setLevel(int level) {
		this.mLevel = level;
		// FIXME 这里根据等级设置相应的体力、精力及经验
		mEnergy.maxValue = 100 + (level - 1) * 10;
		mVigour.maxValue = 2 + level;
		mFullExperience = 200 + (level - 1) * 50;
	}
	
	public int getFullExperience() {
		return mFullExperience;
	}
	
	public int getExperience() {
		return mExperience;
	}
	
	public Status getVigour() {
		return mVigour;
	}
	
	public void useVigour(int use) {
		mVigour.use(use);
	}

	public Status getEnergy() {
		return mEnergy;
	}
	
	public void addEnergy(int energy) {
	    mEnergy.add(energy);
	}
	
	public void useEnergy(int energy) {
	    mEnergy.use(energy);
	}
	
	public void setExpierence(int exp) {
		mExperience = exp;
	}
	
	public void addExpierence(int exp) {
		mExperience += exp;
	}
	
	public int getMoney() {
	    return mMoney;
	}
	
	public int getCash() {
		return mCash;
	}
	
	public void addMoney(int money) {
	    mMoney += money;
	}
	
	public boolean useMoney(int money) {
	    if (money > mMoney || money < 0) {
	        return false;
	    }
	    mMoney -= money;
	    return true;
	}
	
	public void setEnergy(int energy) {
		mEnergy.value = energy;
	}
	
	public void setVigour(int vigour) {
		mVigour.value = vigour;
	}
	
	public void setMoney(int money) {
		mMoney = money;
	}
	
	public void setCash(int cash) {
		mCash = cash;
	}
	
	public Status getStatusByName(String name) {
		if (mVigour.name.equals(name)) {
			return mVigour;
		} else if (mEnergy.name.equals(name)) {
			return mEnergy;
		} else {
			return null;
		}
	}
	
	private class TimerTickService extends TimerTask {

		@Override
		public void run() {
			mVigour.onTick(TICK_INTERVEL);
			mEnergy.onTick(TICK_INTERVEL);
		}
	}
	
	public void setName(String name) {
		mName = name;
	}
	public String getName() {
		return mName;
	}
	
//	public void updateView() {
//		if (mViewHolder == null) {
//			return;
//		}
//		
//		// 经验
//		mViewHolder.experiencePb.setMax(mFullExperience);
//		mViewHolder.experiencePb.setProgress(mExperience);
//		mViewHolder.experienceTv.setText(String.valueOf(mExperience) + "/" + String.valueOf(mFullExperience));
//		
//		// 能量
//		mViewHolder.energyPb.setMax(mEnergy.maxValue);
//		mViewHolder.energyPb.setProgress(mEnergy.value);
//		mViewHolder.energyTv.setText(String.valueOf(mEnergy.value) + "/" + mEnergy.maxValue);
//		
//		// 精力
//		mViewHolder.strengthPb.setMax(mVigour.maxValue);
//		mViewHolder.strengthPb.setProgress(mVigour.value);
//		mViewHolder.strengthTv.setText(String.valueOf(mVigour.value) + "/" + mVigour.maxValue);
//		
//		mViewHolder.coinsTv.setText(String.valueOf(mMoney));
//		
//	}
//	
//	public void bindView(View parent) {
//		if (mViewHolder == null) {
//			mViewHolder = new ViewHolder();
//		}
//		
//		mViewHolder.levelTv = (TextView) parent.findViewById(R.id.tv_level);
//		View pb = parent.findViewById(R.id.pb_level);
//		mViewHolder.experiencePb = (ProgressBar) pb.findViewById(R.id.pb);
//		mViewHolder.experienceTv = (TextView) pb.findViewById(R.id.tv);
//		
//		pb = parent.findViewById(R.id.pb_energy);
//		mViewHolder.energyPb = (ProgressBar) pb.findViewById(R.id.pb);
//		mViewHolder.energyTv = (TextView) pb.findViewById(R.id.tv);
//		mViewHolder.energyTimerTv = (TextView) pb.findViewById(R.id.tv_timer);
//		
//		pb = parent.findViewById(R.id.pb_strength);
//		mViewHolder.strengthPb = (ProgressBar) pb.findViewById(R.id.pb);
//		mViewHolder.strengthTv = (TextView) pb.findViewById(R.id.tv);
//		mViewHolder.strengthTimerTv = (TextView) pb.findViewById(R.id.tv_timer);
//		
//		mViewHolder.coinsTv = (TextView) parent.findViewById(R.id.tv_coins);
//		mViewHolder.goldsTv = (TextView) parent.findViewById(R.id.tv_golds);
//		
//		updateView();
//	}
//	
//	public void updateEnergyTimer(int seconds) {
//		if (mViewHolder != null) {
//			mViewHolder.energyTimerTv.setText(Utilis.fromatTime(seconds));
//		}
//	}
//	
//	public void updateStrengthTimer(int seconds) {
//		if (mViewHolder != null) {
//			mViewHolder.strengthTimerTv.setText(Utilis.fromatTime(seconds));
//		}
//	}
	
//	static class ViewHolder {
//		public TextView levelTv;
//		public ProgressBar experiencePb;
//		public TextView experienceTv;
//		public ProgressBar energyPb;
//		public TextView energyTv;
//		public TextView energyTimerTv;
//		public ProgressBar strengthPb;
//		public TextView strengthTv;
//		public TextView strengthTimerTv;
//		
//		public TextView coinsTv;
//		public TextView goldsTv;
//	}
}
